package com.bytecub.simulate.device.instance;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
  * ByteCub.cn.
  * Copyright (c) 2020-2021 All Rights Reserved.
  * 开放平台授权
  * @author bytecub@163.com  songbin
  * @version Id: AuthHeader.java, v 0.1 2021-02-20  Exp $$
  */
@Slf4j
@Service
public class AuthHeaderService {
    @Autowired com.bytecub.mqtt.domain.config.SimulateHttpProperties httpProperties;
    public MultiValueMap<String, String> buildHeader(){
        String nonce = RandomStringUtils.randomAlphabetic(10);
        String timestamp = "" + System.currentTimeMillis();
        String uncode = httpProperties.getAppKey() + "|" + nonce + "|" + timestamp + "|" + httpProperties.getAppSecret();
        String sign = DigestUtils.md5DigestAsHex(uncode.getBytes());

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("key", httpProperties.getAppKey());
        headers.add("nonce", nonce);
        headers.add("timestamp", timestamp);
        headers.add("sign", sign);

        return headers;
    }
}
